﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SharpSoft.WPF.WriteableBitmapModules
{
    /// <summary>
    /// WriteableBitmap动画模块的基类
    /// </summary>
    public abstract class AnimationModuleBase : IDisposable
    {
        public AnimationModuleBase()
        {
            System.Windows.Media.CompositionTarget.Rendering += CompositionTarget_Rendering;
            stopWatch = new System.Diagnostics.Stopwatch();
            animationTime = new AnimationTime();
        }

        public AnimationModuleBase(WriteableBitmap bitmap) : this()
        {
            RenderTarget = bitmap;
        }
        protected readonly System.Diagnostics.Stopwatch stopWatch;
        private readonly AnimationTime animationTime;
        /// <summary>
        /// 当前操作的图像
        /// </summary>
        public WriteableBitmap RenderTarget { get; set; }
        /// <summary>
        /// 是否已初始化完成
        /// </summary>
        public bool IsInitialized { get; protected set; }
        /// <summary>
        /// 获取当前的Fps
        /// </summary>
        public int Fps
        {
            get
            {
                if (stopWatch.IsRunning)
                {
                    return (int)Math.Ceiling(1000d / animationTime.ElapsedTime.TotalMilliseconds);
                }
                return 0;
            }
        }
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (stopWatch.IsRunning)
            {
                TimeSpan lastTime = animationTime.TotalTime;
                animationTime.TotalTime = stopWatch.Elapsed;
                animationTime.ElapsedTime = animationTime.TotalTime - lastTime;
                this.OnUpdate(animationTime);
                this.OnRender(animationTime);

            }
        }
        /// <summary>
        /// 在派生类中重写，对动画对象的状态进行更新
        /// </summary>
        /// <param name="time"></param>
        protected virtual void OnUpdate(AnimationTime time)
        {

        }
        /// <summary>
        /// 在派生类中实现对每一帧的渲染
        /// </summary>
        /// <param name="time"></param>
        protected virtual void OnRender(AnimationTime time)
        {
            this.OnRenderEnd(time);
        }


        #region CONTROL
        /// <summary>
        /// 初始化动画对象
        /// </summary>
        public virtual void Initialize()
        {
            this.IsInitialized = true;
            this.OnInitialized();
        }

        /// <summary>
        /// 开始动画
        /// </summary>
        public virtual void Start()
        {
            stopWatch.Start();
        }
        /// <summary>
        /// 暂停动画
        /// </summary>
        public virtual void Pause()
        {
            stopWatch.Stop();
        }
        /// <summary>
        /// 停止动画
        /// </summary>
        public virtual void Stop()
        {
            stopWatch.Stop();
            stopWatch.Reset();
            animationTime.TotalTime = TimeSpan.Zero;
            animationTime.ElapsedTime = TimeSpan.Zero;
        }

        /// <summary>
        /// 清空当前帧
        /// </summary>
        /// <param name="color"></param>
        public void Clear(Color color)
        {
            if (this.RenderTarget != null)
            {
                using (this.RenderTarget.GetBitmapContext())
                {
                    this.RenderTarget.Clear(color);
                }
            }

        }
        #endregion

        protected virtual void OnRenderEnd(AnimationTime time)
        {
            RenderEnd?.Invoke(this, new RenderEndEventArgs(time));
        }

        /// <summary>
        /// 在渲染结束后触发
        /// </summary>
        public event RenderEndEventHandler RenderEnd;

        protected virtual void OnInitialized()
        {
            this.Initialized?.Invoke(this, EventArgs.Empty);
        }
        /// <summary>
        /// 在初始化完成后触发
        /// </summary>
        public event EventHandler Initialized;

        protected void OnCompleted()
        {
            this.Stop();
            this.Completed?.Invoke(this, EventArgs.Empty);
        }
        /// <summary>
        /// 在动画完成时触发
        /// </summary>
        public event EventHandler Completed;
        public void Dispose()
        {
            System.Windows.Media.CompositionTarget.Rendering -= CompositionTarget_Rendering;
        }

    }

    public delegate void RenderEndEventHandler(object sender, RenderEndEventArgs e);
    public class RenderEndEventArgs : EventArgs
    {
        public RenderEndEventArgs(AnimationTime time)
        {
            this.Time = time;
        }

        public AnimationTime Time { get; private set; }

    }
    public class AnimationTime
    {
        /// <summary>
        /// 距离上一帧动画的时间
        /// </summary>
        public TimeSpan ElapsedTime { get; set; }
        /// <summary>
        /// 从动画开始的总时间
        /// </summary>
        public TimeSpan TotalTime { get; set; }
    }
}
